package com.oa.xinxi.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oa.login.entity.Emp;
import com.oa.xinxi.biz.NewsBiz;
import com.oa.xinxi.biz.ZhunTWDBiz;
import com.oa.xinxi.entity.News;
import com.oa.xinxi.entity.ZhuanTiWD;

@Controller
@RequestMapping("/wd")
public class WDController {
	@Resource
	private ZhunTWDBiz wdBiz;
	@RequestMapping("/list")
	public String list(Model model,@RequestParam(required=true,defaultValue="1") Integer page) {
		PageHelper.startPage(page, 10);
		List<ZhuanTiWD> list = wdBiz.findAll();
		PageInfo<ZhuanTiWD> p=new PageInfo<ZhuanTiWD>(list);
		model.addAttribute("list", list);
		model.addAttribute("page", p);
		return "/xinxi/wd/list";
	}
	@RequestMapping("/preAdd")
	public String add() {
		return "/xinxi/wd/add";
	}
	@RequestMapping("/add")
	public String save(ZhuanTiWD wd,HttpSession session) {
//		System.out.println("添加");
		Emp emp = (Emp)session.getAttribute("emp");
		wd.setCreator(emp.getEmpName());
		wdBiz.save(wd);
		return "redirect:/wd/list";
	}
	@RequestMapping("/detail/{id}")
	public String detail(Model model,@PathVariable("id") int id) {
		ZhuanTiWD wd = wdBiz.findById(id);
		model.addAttribute("wd", wd);
		return "/xinxi/wd/detail";
	}
	@RequestMapping("/delete/{id}")
	public String deleteById(Model model,@PathVariable("id") int id) {
		wdBiz.deleteById(id);
		return "redirect:/wd/list";
	}
	@RequestMapping("/preUpdate/{id}")
	public String updateById(Model model,@PathVariable("id") int id) {
		ZhuanTiWD wd = wdBiz.findById(id);
		 model.addAttribute("wd", wd);
		return "/xinxi/wd/update";
	}
	@RequestMapping("/update")
	public String DoUpdateById(ZhuanTiWD wd,HttpSession session) {
//		System.out.println(news.getContent());
		Emp emp = (Emp)session.getAttribute("emp");
		wd.setUpdator(emp.getEmpName());
		wdBiz.update(wd);
		return "redirect:/wd/list";
	}
}
